package com.huangjue.backend.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/uploads/**")
//        .addResourceLocations("file:huangjuelan_frontend_back/houduan/online-course-system/uploads/");
//    }

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 1. 硬编码 uploads 的绝对路径（替换为你实际的路径）
        String uploadAbsolutePath = "F:\\springboot\\houduan2\\online-course-system\\uploads";
        System.out.println("Upload directory absolute path: " + uploadAbsolutePath); // 打印验证

        // 2. 配置资源映射
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:" + uploadAbsolutePath + File.separator);
    }
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("*")
                        .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                        .allowedHeaders("*")
                        .allowCredentials(false)
                        .maxAge(3600);
            }
        };
    }
} 